// 按钮状态映射（颜色）
const STATUS_MAP = {
  add: "primary",
  edit: "primary",
  update: "warning",
  view: "success",
  del: "error",
  delete: "error",
  batch: "danger",
  export: "primary",
  excel: "primary",
  import: "primary",
  copy: "warning",
  setmenu:"primary",
  child:"primary",
};

// 按钮样式模式映射（如 text 模式）
const MODE_MAP = {
  add: "",
  edit: "",
  update: "",
  view: "",
  del: "",
  delete: "",
  batch: "",
  export: "",
  excel: "",
  import: "",
  copy: "",
  setmenu:"",
  child:""
};

/**
 * 将路由权限 meta.permission 转换为 Vxe-Table 工具栏按钮配置
 * @param {Array} permissions - 权限列表，如 [{ title: '新增', permission: 'sys:dict:add' }]
 * @returns {Array} 转换后的按钮配置，如 { name: '新增', code: 'add', status: 'primary', mode: '' }
 */
export function permToButtons(permissions: any[]) {
  return permissions.map(
    (item: { permission: any; title: any; inOrOutTable: any }) => {
      const fullPermission = item.permission; // 如 "sys:dict:add"
      const action = fullPermission?.split(":").pop()?.toLowerCase() || ""; // 提取最后部分并转小写

      // 默认值
      let status = "default";
      let mode = "";

      // 匹配 status
      for (const [key, value] of Object.entries(STATUS_MAP)) {
        if (action.includes(key)) {
          status = value;
          break;
        }
      }

      // 匹配 mode
      for (const [key, value] of Object.entries(MODE_MAP)) {
        if (action.includes(key)) {
          mode = value;
          break;
        }
      }

      return {
        name: item.title, // 按钮显示名
        inOrOutTable: item.inOrOutTable == '0' ? true :false ,
        code: action, // 权限动作码
        status, // 颜色类型
        mode, // 按钮样式（如 text）
      };
    }
  );
}

/**
 * 拆分按钮为：工具栏按钮 和 表格内操作按钮
 */
export function splitButtonsByLocation(buttons: any[]) {
  const toolbarButtons = buttons.filter((btn) => !btn.inOrOutTable); // 不在表格内 → 工具栏
  const inTableButtons = buttons.filter((btn) => btn.inOrOutTable); // 在表格内 → 操作列
  return { toolbarButtons, inTableButtons };
}
